<?
include ('../inc/config.php');

//$_CONF->debugTypes = array('ALL');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?=$_MSG['configurator_title'];?></title>
<style>@import '../css/common.css';</style>
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/Lightbox.js"></script>
<style>
	#newFieldsRowTable th {
		font-size: 13px; font-weight: normal; background: #ccc; text-align: left; 
	}
</style>
</head>
<body id="contents">

<div id="contentsContent">

	<div id="contentHeader">
		<div id="toolBar" style="padding: 5px">
			<input type="button" value="AGGIORNA" onClick="window.location='<?=$_SERVER['PHP_SELF'];?>?configuring=<?=$_GET['configuring']?>'"> <input type="submit" name="via" onClick="document.configurator.submit();" value="SAVE PREFERENCES"> 
		</div>
		<h1>Database configurator <span class="description"><?=$_MSG['configurator_description']?></span></h1>
		
		<div class="contentPad">
			<strong>Tabella: </strong> &nbsp;<select onChange="window.location='configurator.php?configuring='+this.options[this.selectedIndex].value">
			<option value="--">--</option>
			<?
			$tables = $_DB->getTablesName();
			foreach($tables as $table) {
				$title = $_CONF->get("CONFIG>TABLES>TABLE.name=$table.title");
				echo '<option value="'.$table.'" '.(($_GET['configuring']==$table)?'selected':'').'>'.(($title)?$title:$table).'</option>';
			}
			?>
			</select>
			<?
			if ($_GET['configuring'] != "--" && $_GET['configuring'] != null) {
				echo '<input type="button" value="Vai alla tabella" onclick="window.location = \'../content.php?table='.$_GET['configuring'].'\'">';
			}
			?>
		</div>
	</div>


<? if($_GET['action'] == "SAVING_PREFERENCES") {
	
	?><div id="conf_messager"><h3><a href="#" onClick="$('conf_messager_debug').style.display = 'block';"><strong>+</strong></a> Salvato</h3><div id="conf_messager_debug"><?
	
	$myTable = "CONFIG>TABLES>TABLE.name=".$_POST['TABLE'];
	$_CONF->setValue($myTable.".name", $_POST['TABLE']);
	$_CONF->setValue($myTable.".title", $_POST['title']);
	$_CONF->setValue($myTable.".visible", $_POST['visible']);
	
	if($_POST['dataPerPage'] != "") $_CONF->setValue($myTable.".dataPerPage", $_POST['dataPerPage']);
	if($_POST['orderField'] != "") $_CONF->setValue($myTable.".orderField", $_POST['orderField']);
	if($_POST['orderDir'] != "") $_CONF->setValue($myTable.".orderDir", $_POST['orderDir']);
	if($_POST['description'] != "") {
		$_CONF->appendNewNode($myTable.".node", $_CONF->createNode("DESCRIPTION"));
//		$_CONF->setValueByKey($myTable.">DESCRIPTION", "['nodeValue']", $_POST['description']);
		$_CONF->setValue($myTable.">DESCRIPTION.nodeValue", $_POST['description']);
	}
	
	$_FIELDS = $_DB->getFields($_POST['TABLE']);
	foreach($_FIELDS as $field => $type) {
		
		
		
		// Devo settare qualche configurazione per questo campo
		$_CONF->get($myTable.">FIELDS>FIELD.name=$field", false, array('createIfNull'));
		$myField = $myTable . ">FIELDS>FIELD.name=$field";
		
		echo "Titolo per il campo " . $field . " => " . $_POST[$field.'_title'] . " ($myField)<br>";
		
		$_CONF->setValue($myField.".title", $_POST[$field.'_title']);
		$_CONF->setValue($myField.".visible", $_POST[$field.'_visible']);
		$_CONF->setValue($myField.".adminField", $_POST[$field.'_adminField']);
		$_CONF->setValue($myField.".multiple", $_POST[$field.'_multiple']);
		$_CONF->setValue($myField.".type", $_POST[$field.'_type']);
		$_CONF->setValueByKey($myTable.">FIELDS>FIELD.name=$field>DESCRIPTION", "['nodevalue']", $_POST[$field.'_description']);
		$_CONF->setValueByKey($myTable.">FIELDS>FIELD.name=$field>DEFAULT", "['nodevalue']", $_POST[$field.'_default']);
		
	}
	
	/* Controllo le estensioni e i plugin */
	foreach($_POST as $key => $value) {
		if(strpos($key, 'plugin') !== false && strpos($key, 'ref') === false) {
			list($pluginIndex, $var, $remove) = explode("_", $key);
			if($remove) {
				$childs = $_CONF->get($myTable . ">PLUGINS");
				for($i=0; $i<count($childs); $i++) {
					if(md5($_CONF->get("PLUGIN.name", $childs[$i])) == $var) $_CONF->remove($myTable . ">PLUGINS>PLUGIN.name=".$_CONF->get("PLUGIN.name", $childs[$i]));
				}
			} else {
				$_CONF->get($myTable . ">PLUGINS>PLUGIN.name=".$_POST[$pluginIndex . '_name'], false, array('createIfNull'));
				$_CONF->setValue($myTable . '>PLUGINS>PLUGIN.name='.$_POST[$pluginIndex . '_name'].'.value', $_POST[$pluginIndex . '_ref']);	
			}
		}
	}
	
	_log("Configurator: Salvata nuova configurazione per ".$_POST['TABLE']);
	$_CONF->saveBuiltXML();
	
	//$_CONF->buildXML();
	
	echo "<strong>RESULT:<br></strong><textarea style=width:100%;height:400px>";
	print_r($_CONF->XMLbuild);
	echo "</textarea>";	
	
	echo "</div></div>";
	
} ?>
	
	<? if ($_GET['configuring']) { ?>
	<div id="contentWrapper">
			
		<form name="configurator" id="configurator_form" method="post" action="configurator.php?action=SAVING_PREFERENCES&configuring=<?=$_GET['configuring']?>">
		<input type="hidden" name="TABLE" value="<?=$_GET['configuring']?>" />
		<table width="100%" cellpadding="6" cellspacing="0" bgcolor="#eeeeee">
		<?
		$_TABLE = new table($_GET['configuring']);
		$_SESSION['configuring_table'] = $_TABLE->table;
		
		$title = $_CONF->get("TABLE.title", $_TABLE->CONF);
		$visible = $_CONF->get("TABLE.visible", $_TABLE->CONF);
		$dataPerPage = $_CONF->get("TABLE.dataPerPage", $_TABLE->CONF);
		$orderField = $_CONF->get("TABLE.orderField", $_TABLE->CONF);
		$orderDir = $_CONF->get("TABLE.orderDir", $_TABLE->CONF);
		$description = $_CONF->get("TABLE>DESCRIPTION.nodeValue", $_TABLE->CONF);
		?>
		<tr bgcolor="c2c2c2">
			<td colspan="2">Tabella: <span style="font-size: 22px"><?=$_GET['configuring']?></span></td>
		</tr>
		<tr bgcolor="#f8f8f8">
			<td>
				<table cellspacing="0" cellpadding="3">
					<tr>
						<td align="right"><strong>TITOLO</strong></td><td><input name="title" type="text" value="<?=(($title)?$title:$_GET['configuring'])?>"></td>
					</tr>
					<tr bgcolor="#ffffff">
						<td align="right"><strong>SETUP</strong></td>
						<td align="right" nowrap="nowrap"><strong>Visibile</strong></td><td><select name="visible"><option value="true" <?=(($visible == "true")?"selected":"")?>>SI</option><option value="false" <?=(($visible == "false")?"selected":"")?>>NO</option></select></td>
						<td align="right" nowrap="nowrap"><strong>Dati per pagina</strong></td><td><input name="dataPerPage" size="3" maxlength="2" type="text" value="<?=$dataPerPage?>"></td>
						<td align="right" nowrap="nowrap"><strong>Ordina per campo</strong></td><td><select name="orderField" id="fieldsSelect">
						<option value="">--</option>
						<?
						foreach($_DB->getFields($_TABLE->table) as $name => $type) {
							echo '<option value="'.$name.'" '.(($orderField == $name)?'selected':'').'>'.$name.'</option>';
						}
						?>
						</select>
						</td>
						<td align="right" nowrap="nowrap"><strong>Verso d'ordinamento</strong></td><td><select name="orderDir"><option value="">--</option><option value="ASC" <?=(($orderDir == "ASC")?"selected":"")?>>Crescente</option><option value="DESC" <?=(($orderDir == "DESC")?"selected":"")?>>Decrescente</option></select></td>
					</tr>
					<tr bgcolor="#f2f2f2" valign="top">
						<td align="right"><strong>DESCRIZIONE</strong></td>
						<td colspan="9"><textarea style="width: 100%" name="description"><?=$description?></textarea></td>
					</tr>
				</table>
			</td>
		</tr>
			
		<tr>
			<td colspan="7">
			<div>
			<table cellpadding="7" width="100%" cellspacing="0" id="newFieldsRowTable" bgcolor="#FFFFFF">
			<? $_FIELDS = $_CONF->get("TABLE>FIELDS.childNodes", $_TABLE->CONF); ?>
			<tr>
				<th>Key</th>
				<th>Nome</th>
				<th>Visible</th>
				<th>Campo admin</th>
				<th>Default</th>
				<th>Multiplo</th>
				<th>Tipo</th>
				<th>Cross field</th>
				<th>Virtual Cross</th>
			</tr>
			<?
			$_DBFIELDS = $_DB->getFields($_GET['configuring']);
			foreach($_DBFIELDS as $field => $type) {
				
				$_FIELD 		= $_CONF->get("TABLE>FIELDS>FIELD.name=" . $field . ".node", $_TABLE->CONF);
				
				$title 			= (($_CONF->get("FIELD.title", $_FIELD)) ? $_CONF->get("FIELD.title", $_FIELD) : $field);
				$visible 		= (($_CONF->get("FIELD.visible", $_FIELD)) ? $_CONF->get("FIELD.visible", $_FIELD) : 'true');
				$adminField		= (($_CONF->get("FIELD.adminField", $_FIELD)) ? $_CONF->get("FIELD.adminField", $_FIELD) : 'false');
				$defaultValue 	= (($_CONF->get("FIELD>DEFAULT.nodeValue", $_FIELD)) ? $_CONF->get("FIELD>DEFAULT.nodeValue", $_FIELD) : '');
				$multiple 		= (($_CONF->get("FIELD.multiple", $_FIELD)) ? $_CONF->get("FIELD.multiple", $_FIELD) : 'false');
				$type 			= (($_CONF->get("FIELD.type", $_FIELD)) ? $_CONF->get("FIELD.type", $_FIELD) : 'db');
				$cross 			= (($_CONF->get("FIELD>CROSS.node", $_FIELD)) ? $_CONF->get("FIELD>CROSS.node", $_FIELD) : 'false');
				$virtualCross 	= (($_CONF->get("FIELD>VIRTUALCROSS.childNodes", $_FIELD)) ? $_CONF->get("FIELD>VIRTUALCROSS.childNodes", $_FIELD) : 'false');
				
				echo '<tr>
					<td><strong>'.$field.'</strong></td>
					<td><strong><input type="text" name="'.$field.'_title" value="'.stripslashes($title).'" /></strong></td>
					<td><select name="'.$field.'_visible"><option '.(($visible != 'false') ? 'selected' : '').' value="true">SI</option><option value="false" '.(($visible == 'false') ? 'selected' : '').'>NO</option></select></td>
					<td><select name="'.$field.'_adminField"><option '.(($adminField != 'false') ? 'selected' : '').' value="true">SI</option><option value="false" '.(($adminField == 'false') ? 'selected' : '').'>NO</option></select></td>
					<td><input type="text" name="'.$field.'_default" value="'.stripslashes($defaultValue).'" /></td>
					<td><select name="'.$field.'_multiple"><option '.(($multiple != 'false') ? 'selected' : '').' value="true">SI</option><option value="false" '.(($multiple == 'false') ? 'selected' : '').'>NO</option></select></td>
					<td><select name="'.$field.'_type"><option '.(($type == 'db') ? 'selected="selected"' : '').' value="db">Database</option><option value="filesystem" '.(($type == 'filesystem') ? 'selected="selected"' : '').'>Filesystem</option></select></td>
					<td align="center">'.(($cross != 'false') ? '<a href="#" onclick="new Lightbox.Open({id: \'Confirm_alert\', width: 350, height: 200, reqUrl: \'../inc/ajaxRequests.php?action=removeCrossField&table='.$_TABLE->table.'&field='.$field.'\'});return false;"><img src="../i/rem.gif"></a> <a href="javascript:Animations.showCrossfieldPopup(\''.$field.'\');"><img src="../i/edit.gif"></a>' : '<a href="javascript:Animations.showCrossfieldPopup(\''.$field.'\');"><img src="../i/add.gif"></a>').'</td>
					<td align="center">'.(($virtualCross != 'false') ? '<a href="#" onclick="new Lightbox.Open({id: \'Confirm_alert\', width: 350, height: 140, reqUrl: \'../inc/ajaxRequests.php?action=removeVirtualField&table='.$_TABLE->table.'&field='.$field.'\'});return false;"><img src="../i/rem.gif"></a> <a href="javascript:Animations.showVirtualCrossfieldPopup(\''.$field.'\');"><img src="../i/edit.gif"></a>' : '<a href="javascript:Animations.showVirtualCrossfieldPopup(\''.$field.'\');"><img src="../i/add.gif"></a>').'</td>
				</tr>';
			}
			
			
			?></table><!-- input type="button" value="Aggiungi campo da configurare" onClick="FF.configurator.addFieldSettingRow();"><br--><?
			
			// Metto la riga per i campi provvisoria e nascosta
			?>
			</div>
			
		<br></td></tr>
		<tr bgcolor="lightblue">
			<td colspan="2">Plugins: <span style="font-size: 22px"><?=$_GET['configuring']?></span></td>
		</tr>
		<tr><td>
			<table cellpadding="5" cellspacing="1" bgcolor="#f3f3f3" id="pluginTable">
			<?
			$exts = $_CONF->get("TABLE>PLUGINS.childNodes", $_TABLE->CONF);
			if(count($exts) > 0 && $exts !== false) {
				echo '<tr><td>&nbsp;</td><td><strong>Nome</strong></td><td><strong>Filename</strong></td></tr>';
				foreach($exts as $key => $value) {
					echo '<tr>
						<td><input type="button" value="remove" onclick="FF.configurator.removePlugin(this, \''.md5($_CONF->get("PLUGIN.name", $value)).'\');"></td>
						<td><input type="text" value="'.$_CONF->get("PLUGIN.name", $value).'" name="plugin'.$key.'_name" /></td>
						<td><input type="text" value="'.$_CONF->get("PLUGIN.value", $value).'" name="plugin'.$key.'_ref" /></td>
					</tr>';
				}
			} else {
				echo '<tr><td colspan="3">Non ci sono plugin per questa tabella</td></tr>';
			}
			?>
			</table>
			<input type="button" name="addPlugin" value="Aggiungi plugin" onClick="FF.configurator.addPluginRow();"><br>
<br>
		</td></tr>
		</table>
		<br>
		</form>
	
	</div>
	<!-- fine contentWrapper -->
	<? } ?>

</div>
</body>
</html>




